#pragma once

#include <rclcpp/rclcpp.hpp>

#include <NvInfer.h>
#include <opencv2/opencv.hpp>
#include <sys/stat.h>
#include <unistd.h>

class NvLogger : public nvinfer1::ILogger {
public:
    nvinfer1::ILogger::Severity reportableSeverity;

    explicit NvLogger(nvinfer1::ILogger::Severity severity = nvinfer1::ILogger::Severity::kINFO)
        : reportableSeverity(severity)
        , logger_(rclcpp::get_logger("NvLogger"))
    {
    }

    void log(nvinfer1::ILogger::Severity severity, const char* msg) noexcept override
    {
        switch (severity) {
        case nvinfer1::ILogger::Severity::kINTERNAL_ERROR:
            RCLCPP_ERROR(logger_, "nvinfer INTERNAL_ERROR: %s", msg);
            break;
        case nvinfer1::ILogger::Severity::kERROR:
            RCLCPP_ERROR(logger_, "nvinfer ERROR: %s", msg);
            break;
        case nvinfer1::ILogger::Severity::kWARNING:
            RCLCPP_WARN(logger_, "nvinfer WARNING: %s", msg);
            break;
        case nvinfer1::ILogger::Severity::kINFO:
            RCLCPP_INFO(logger_, "nvinfer INFO: %s", msg);
            break;
        default:
            RCLCPP_INFO(logger_, "nvinfer VERBOSE: %s", msg);
            break;
        }
    }

private:
    rclcpp::Logger logger_;
};


inline static float clamp(float val, float min, float max)
{
    return val > min ? (val < max ? val : max) : min;
}
